package com.lecture.java8.excel.nhjy;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Demo01 {
    public static void main(String[] args) {
        String fileName = "D:\\Program Files (x86)\\Tencent\\WeChat Files\\wxid_bdikx7q2lx2822\\FileStorage\\File\\2025-03\\数据\\公有数据\\风电总出力预测\\处理后_2024-05-01_风电总出力预测.xlsx";
        List<WindPowerOutputData> dataList = new ArrayList<>();

        EasyExcel.read(fileName, new AnalysisEventListener<Object>() {
            private String dateStr;
            private String name;

            List<String> rowData = new ArrayList<>();
            @Override
            public void invoke(Object data, AnalysisContext context) {
                HashMap<String, String> data1 = (HashMap<String, String>) data;
                List<String> row = new ArrayList<>(data1.values());

                if (context.readRowHolder().getRowIndex() == 1) {
                    // 第三行，解析时间
                    /*dateStr = row.get(0).replace("时间:", "");
                    name = row.get(1);*/
                    rowData = row;
                } else {
                    // 从第二行开始，解析数据
                    if (row.get(0) != null && !"".equals(row.get(0))) {
                        dateStr = row.get(0).replace("时间:", "").replace("：","");
                    }
                    name = row.get(1);
                    for (int i = 2; i < row.size(); i++) {
                        WindPowerOutputData outputData = new WindPowerOutputData();
                        outputData.setDateStr(dateStr + " " + rowData.get(i));
                        outputData.setName(name);
                        outputData.setValue(Double.parseDouble(row.get(i)));
                        dataList.add(outputData);
                    }
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                // 解析完成后的操作
                System.out.println("解析完成，共解析 " + dataList.size() + " 条数据");
                for (WindPowerOutputData data : dataList) {
                    System.out.println(data);
                }
            }
        }).sheet().doRead();
    }
}
class WindPowerOutputData {
    private String dateStr;
    private String name;
    private double value;

    // Getters and Setters
    public String getDateStr() {
        return dateStr;
    }

    public void setDateStr(String dateStr) {
        this.dateStr = dateStr;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "WindPowerOutputData{" +
                "dateStr='" + dateStr + '\'' +
                ", name='" + name + '\'' +
                ", value=" + value +
                '}';
    }
}
